<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 无畏契约 Wiki</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', sans-serif;
            line-height: 1.6;
            background: linear-gradient(135deg, #0f1923 0%, #ff4655 100%);
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: fixed;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .navbar {
            background-color: #ff4655;
            padding: 1rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .nav-container {
            width: 100%;
            padding: 0 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logo {
            color: white;
            font-size: 1.5rem;
            font-weight: bold;
            text-decoration: none;
        }

        .nav-links {
            display: flex;
            gap: 2rem;
        }

        .nav-links a {
            color: white;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s;
        }

        .nav-links a:hover {
            color: #ececec;
        }

        .login-container {
            width: 50%;
            margin: 8rem auto;
            padding: 5rem;
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 16px;
            box-shadow: 0 6px 15px rgba(0,0,0,0.15);
        }

        .login-form {
            display: flex;
            flex-direction: column;
            gap: 3rem;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 0.8rem;
        }

        .form-group input {
            padding: 1.5rem;
            border: 2px solid #ddd;
            border-radius: 12px;
            font-size: 1.2rem;
            transition: border-color 0.3s;
        }

        .form-group label {
            font-weight: bold;
            color: #333;
        }

        .form-group input:focus {
            border-color: #ff4655;
            outline: none;
        }

        .remember-me {
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .remember-me input[type="checkbox"] {
            width: 16px;
            height: 16px;
        }

        .login-button {
            background-color: #ff4655;
            color: white;
            padding: 1.2rem;
            border: none;
            border-radius: 8px;
            font-size: 1.2rem;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 1rem;
        }

        .login-button:hover {
            background-color: #ff5c69;
        }

        .form-divider {
            text-align: center;
            margin-top: 2rem;
            color: #666;
        }

        .register-link {
            color: #ff4655;
            text-decoration: none;
            margin-left: 0.5rem;
            font-weight: bold;
        }

        .register-link:hover {
            text-decoration: underline;
        }

        .register-form {
            margin-top: 2rem;
        }

        .secondary-button {
            background-color: #666;
            color: white;
            padding: 1.2rem;
            border: none;
            border-radius: 8px;
            font-size: 1.2rem;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 1rem;
            width: 100%;
        }

        .secondary-button:hover {
            background-color: #777;
        }

        .error-message {
            color: #ff4655;
            font-size: 0.9rem;
            margin-top: 0.3rem;
            display: none;
        }

        .footer {
            background-color: #333;
            color: white;
            padding: 2rem 0;
            margin-top: auto;
        }

        .footer-content {
            width: 100%;
            padding: 0 2rem;
            text-align: center;
        }

        @media (max-width: 768px) {
            .nav-links {
                display: none;
            }

            .login-container {
                width: 90%;
                margin: 2rem auto;
                padding: 2rem;
            }
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <div class="nav-container">
            <a href="index.html" class="logo">无畏契约 Wiki</a>
            <div class="nav-links">
                <a href="index.html">首页</a>
                <a href="#">皮肤图鉴</a>
                <a href="#">武器数据</a>
                <a href="login.html">登录</a>
            </div>
        </div>
    </nav>

    <div class="login-container">
        <form class="login-form" id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
                <div class="error-message" id="usernameError">请输入用户名</div>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
                <div class="error-message" id="passwordError">请输入密码</div>
            </div>
            <div class="remember-me">
                <input type="checkbox" id="rememberMe" name="rememberMe">
                <label for="rememberMe">记住我</label>
            </div>
            <div class="error-message" id="apiError" style="display: none; margin-bottom: 10px;"></div>
            <button type="submit" class="login-button">登录</button>
        </form>
        <div class="form-divider">
            <span>还没有账户？</span>
            <a href="#" id="showRegister" class="register-link">立即注册</a>
        </div>
        <form class="register-form" id="registerForm" style="display: none;">
            <div class="form-group">
                <label for="registerUsername">用户名</label>
                <input type="text" id="registerUsername" name="registerUsername" required>
                <div class="error-message" id="registerUsernameError">请输入用户名</div>
            </div>
            <div class="form-group">
                <label for="registerPassword">密码</label>
                <input type="password" id="registerPassword" name="registerPassword" required>
                <div class="error-message" id="registerPasswordError">请输入密码</div>
            </div>
            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" name="confirmPassword" required>
                <div class="error-message" id="confirmPasswordError">请确认密码</div>
            </div>
            <button type="submit" class="login-button">注册</button>
            <button type="button" class="secondary-button" id="backToLogin">返回登录</button>
        </form>
    </div>

    <footer class="footer">
        <div class="footer-content">
            <p>&copy; 2024 无畏契约 Wiki. 本网站仅供学习交流使用。</p>
        </div>
    </footer>

    <script>
        // 切换表单显示
        document.getElementById('showRegister').addEventListener('click', function(e) {
            e.preventDefault();
            document.getElementById('loginForm').style.display = 'none';
            document.getElementById('registerForm').style.display = 'block';
        });

        document.getElementById('backToLogin').addEventListener('click', function() {
            document.getElementById('registerForm').style.display = 'none';
            document.getElementById('loginForm').style.display = 'block';
        });

        // 添加API基础URL配置
        const API_BASE_URL = 'http://localhost:8080';

        // 注册表单验证和提交
        document.getElementById('registerForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('registerUsername').value;
            const password = document.getElementById('registerPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const registerUsernameError = document.getElementById('registerUsernameError');
            const registerPasswordError = document.getElementById('registerPasswordError');
            const confirmPasswordError = document.getElementById('confirmPasswordError');
            
            // 重置错误提示
            registerUsernameError.style.display = 'none';
            registerPasswordError.style.display = 'none';
            confirmPasswordError.style.display = 'none';
            
            let hasError = false;
            
            if (!username) {
                registerUsernameError.style.display = 'block';
                hasError = true;
            }
            
            if (!password) {
                registerPasswordError.style.display = 'block';
                hasError = true;
            }
            
            if (!confirmPassword) {
                confirmPasswordError.style.display = 'block';
                confirmPasswordError.textContent = '请确认密码';
                hasError = true;
            } else if (password !== confirmPassword) {
                confirmPasswordError.style.display = 'block';
                confirmPasswordError.textContent = '两次输入的密码不一致';
                hasError = true;
            }
            
            if (!hasError) {
                try {
                    const response = await fetch(`${API_BASE_URL}/api/users/register`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            username,
                            password
                        })
                    });

                    const data = await response.text(); // 直接获取响应的文本内容

                    if (response.ok) {
                        alert(data); // 显示后端返回的成功信息
                        document.getElementById('registerForm').style.display = 'none';
                        document.getElementById('loginForm').style.display = 'block';
                    } else {
                        alert(data); // 显示后端返回的错误信息
                    }
                } catch (error) {
                    console.error('注册错误：', error);
                    alert('无法连接到服务器，请确保后端服务已启动');
                }
            }
        });

        // 登录表单验证和提交
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');
            
            // 重置错误提示
            usernameError.style.display = 'none';
            passwordError.style.display = 'none';
            
            let hasError = false;
            
            if (!username) {
                usernameError.style.display = 'block';
                hasError = true;
            }
            
            if (!password) {
                passwordError.style.display = 'block';
                hasError = true;
            }
            
            if (!hasError) {
                try {
                    const response = await fetch(`${API_BASE_URL}/api/users/login`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            username,
                            password
                        })
                    });

                    const data = await response.text(); // 直接获取响应的文本内容

                    if (response.ok) {
                        alert(data); // 显示后端返回的成功信息
                        window.location.href = 'index.html'; // 跳转到首页
                    } else {
                        alert(data); // 显示后端返回的错误信息
                    }
                } catch (error) {
                    console.error('登录错误：', error);
                    alert('无法连接到服务器，请确保后端服务已启动');
                }
            }
        });
    </script>
</body>
</html>